package com.gu.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gu.entity.CrmBanner;
import com.gu.service.AdminBannerService;
import entity.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import javax.annotation.Resource;

/**
 * Description:  banner 轮播图后台管理系统
 *
 * @Autor: Rolin
 * datetime: 05-30-22:35
 */
@RestController
@CrossOrigin
@Api(tags = "轮播图插图后台管理系统")
@RequestMapping("/serviceCms")
public class AdminBannerController {
    //crud 咯
    @Resource
    private AdminBannerService adminBannerService;

    @ApiOperation("后台修改banner图信息")
    @PutMapping("/update/{id}")
    public CommonResult updateBannerById(@PathVariable("id") String id,
                                         @RequestBody CrmBanner crmBanner) {
        crmBanner.setId(id);
        boolean result = adminBannerService.updateById(crmBanner);
        return result ? CommonResult.success().data("result",true) : CommonResult.error();
    }

    @ApiOperation("后台删除banner图信息")
    @DeleteMapping("/delete/{id}")
    public CommonResult deleteById(@PathVariable("id") String id){
        boolean result = adminBannerService.removeById(id);
        return result ? CommonResult.success().data("result",true): CommonResult.error();
    }

    @ApiOperation("后台新增banner图信息")
    @PostMapping("/addBanner")
    public CommonResult insertBanner(@RequestBody CrmBanner crmBanner){
        boolean result = adminBannerService.save(crmBanner);
        return result ? CommonResult.success() : CommonResult.error();
    }

    @ApiOperation("获取所有的轮播图信息")
    @PostMapping("/getCourseListPage/{current}/{limit}")
    public CommonResult getAllBannerByPage(@PathVariable("current") Long current,
                                           @PathVariable("limit") Long limit){
        Page<CrmBanner>params =  adminBannerService.getBannersByPage(current,limit);
        List<CrmBanner> bannerList = params.getRecords();
        long total = params.getTotal();
        return bannerList.size()>0 ? CommonResult.success().data("bannerList",bannerList).data("rows",total):CommonResult.error();
    }

    @ApiOperation("根据Id查询banner")
    @GetMapping("getBannerById/{id}")
    public CommonResult getBannerById(@PathVariable("id")String id){
        CrmBanner banner = adminBannerService.getById(id);
        return !StringUtils.isEmpty(banner.getTitle())? CommonResult.success().data("banner",banner):CommonResult.error();
    }

}
